#!/usr/bin/bash

SWITCHDEV=$SIM_SWITCHINFO/devices

readFile ()
{
  FILE=$1
  if [ -f $FILE ]; then
    cat $FILE
  else
    printf "-"
  fi
}

printDebug ()
{
  local LEVEL=$1
  local MESSAGE="$2"
  if [ "$LEVEL" = "0" ]; then
    echo "  `date +%H:%M:%S`: $MESSAGE"
    if [ "$SERVER_NAME" != "" ]; then
      echo "<BR>"
    fi
  elif [ "$SIM_DEBUGGING" = "yes" ]; then
    echo "    Debug `date +%H:%M:%S`: $MESSAGE"
    if [ "$SERVER_NAME" != "" ]; then
      echo "<BR>"
    fi
  fi
}

getIntStatus ()
{
  local SWITCHIP=$1
  local SWITCHCOMM=$2
  local INTNO=$3
  INTOPER=`$SIM_SNMPGET -Ovq -v1 -c $SWITCHCOMM $SWITCHIP .1.3.6.1.2.1.2.2.1.8.$INTNO`
  case $INTOPER in
    (up) printf "#99EE99" ;;
    (down)
      if [ "`$SIM_SNMPGET -Ovq -v1 -c $SWITCHCOMM $SWITCHIP .1.3.6.1.2.1.2.2.1.7.$INTNO`" = "down" ]; then
        printf "#33FFFF" 
      else
        printf "#FF6600"
      fi
      ;;
    (*)    printf "grey" ;;
  esac
}

testSwitchName ()
{
  local SWITCHNAME=$1
  local SWITCHIP=`cat $SWITCHDEV/$SWITCHNAME/switchip 2>/dev/null | awk '{ print $1 }'`
  local SWITCHCOMM=`cat $SWITCHDEV/$SWITCHNAME/switchip 2>/dev/null | awk '{ print $2 }'`
  local SYSNAME=`$SIM_SNMPGET -Ovq -v1 -r 2 -c $SWITCHCOMM $SWITCHIP .1.3.6.1.2.1.1.5.0 | awk -F. '{ print $1 }' | tr [A-Z] [a-z]`
  printf "$SYSNAME"
}

getSnmpLoc ()
{
  SWITCHNAME=$1
  SWITCHIP=`cat $SWITCHDEV/$SWITCHNAME/switchip 2>/dev/null | awk '{ print $1 }'`
  IPDIR=$SIM_IPDATAB/`echo $SWITCHIP| sed 's#\.#/#g'`
  if [ -f $IPDIR/firstmac.txt ]; then
    FIRSTMAC=`cat $IPDIR/firstmac.txt`
    if [ -f $SIM_MACDATAB/$FIRSTMAC/snmpLocation ]; then
      SNMPLOC=`cat $SIM_MACDATAB/$FIRSTMAC/snmpLocation`
      printf "$SNMPLOC"
    fi
  fi
}
displaySwitches ()
{
  local SWITCH=$1
  CACHEFILE=$SIM_TMPDIR/switches-$SECTION-$REMOTE_USER
  if [ -d $SWITCHDEV ]; then
    if [ "$SWITCH" = "none" ]; then
      echo "<OPTION VALUE=$SWITCH>List</OPTION>" > $CACHEFILE
      ls $SWITCHDEV | while read SWITCHNAME
      do
        if [ -d $SWITCHDEV/$SWITCHNAME ]; then
          echo ". <A HREF=$SCRIPT_NAME?switch+$ACTION+$SWITCHNAME>$SWITCHNAME</A> ."
          SNMPLOC=`getSnmpLoc $SWITCHNAME`
          echo "<OPTION VALUE=$SWITCHNAME>$SWITCHNAME --- $SNMPLOC</OPTION>" >> $CACHEFILE
	else
	  echo ". <FONT COLOR=red>$SWITCHNAME</FONT> ."
	  rm $SWITCHDEV/$SWITCHNAME
	fi
      done
    else
      echo "Devices ($SWITCH):
              <FORM method=post Action=$SCRIPT_NAME?switch+display+form>
	      <SELECT NAME=switch>"
      cat $CACHEFILE
      echo "</SELECT> <INPUT TYPE=submit Value=Select> </FORM>"
    fi
  else
    echo "Oops, no switchinfo dir found...."
  fi
}

displayRaw ()
{
  local SWITCH=$1
  echo "RAW"
  echo "<PRE>"
  cat $SWITCHDEV/$SWITCH/mac-* | sort -nk 2 | while read SWITCH INT MAC DATE VLAN NAME INT
  do
    echo "\"$SWITCH\",\"$INT\",\"$MAC\",\"$DATE\",\"$VLAN\",\"$NAME\",\"$INT\""
  done
  echo "</PRE>"
}
testIpAddr ()
{
  local TMAC=$1
  local MACDIR="$SIM_MACDATAB/$TMAC"
  if [ -f $MACDIR/dhcp-ipaddr.txt ]; then
    IPADDR=`cat $MACDIR/dhcp-ipaddr.txt`
    NETDIR=$SIM_IPDATAB/`echo $IPADDR| sed 's#\.#/#g'`
    if [ -f "$NETDIR/arpmac.txt"]; then
      ARPMAC=`cat $NETDIR/arpmac.txt`
      if [ $ARPMAC != $TMAC ]; then
        rm $MACDIR/dhcp-ipaddr.txt
      else
        echo $IPADDR
      fi
    fi
  else
    ls -r $MACDIR/*-ipaddr-* 2>/dev/null | while read FILE
    do
      cat $FILE
      break
    done
  fi
}

testNodeTypeColor ()
{
  local NODETYPE=$1
  NUM=1
  local TYPECOLOR=""
  while [ "${NETNAME[$NUM]}" != "" ]
  do
    if [ "$NODETYPE" = "${NETNAME[$NUM]}" ]; then
      TYPECOLOR=${NETBG[$NUM]}
    fi
    NUM=`expr $NUM + 1`
  done
  if [ "$TYPECOLOR" = "" ]; then
    TYPECOLOR=${NETBG[0]}
  fi
  printf $TYPECOLOR
}

dispNodeInfo ()
{
  local IPADDR=$1
  local TMAC=$2
  local SWITCHNAME=$3
  local SWITCHPORT=$4
  local VLANNO=$5
  #echo "<TR><TD COLSPAN=5>IPADDR=$IPADDR TMAC=$TMAC SWITCHNAME=$SWITCHNAME SWITCHPORT=$SWITCHPORT VLANNO=$VLANNO.</TD></TR>"
  NETDIR=$SIM_IPDATAB/`echo $IPADDR| sed 's#\.#/#g'`
  NODETYPE=`readFile $NETDIR/nodetype.txt`
  TYPECOLOR=`testNodeTypeColor $NODETYPE`

  SIPMANMANTYPE=`readFile $NETDIR/managementtype.txt`
  case $SIPMANMANTYPE in
    (${MANTYPE[0]}) MANBGCOLOR=${MANCOLOR[0]} ; DMANSTR=${MANSTR[0]} ;;
    (${MANTYPE[1]}) MANBGCOLOR=${MANCOLOR[1]} ; DMANSTR=${MANSTR[1]} ;;
    (${MANTYPE[2]}) MANBGCOLOR=${MANCOLOR[2]} ; DMANSTR=${MANSTR[2]} ;;
    (${MANTYPE[3]}) MANBGCOLOR=${MANCOLOR[3]} ; DMANSTR=${MANSTR[3]} ;;
    (*)             MANBGCOLOR=orange ; DMANSTR=? ;;
  esac
  #   echo "-$SIPMANMANTYPE-$MANBGCOLOR-"
  SIPMANRECTYPE=`readFile $NETDIR/sipmanrectype.txt`
  case $SIPMANRECTYPE in
    (${RECNAME[1]}) SIPMANTYPEBGCOLOR=${RECCOLOR[1]} ; SIPMANTYPESTR=${RECTYPESTR[1]} ;;
    (${RECNAME[2]}) SIPMANTYPEBGCOLOR=${RECCOLOR[2]} ; SIPMANTYPESTR=${RECTYPESTR[2]} ;;
    (${RECNAME[3]}) SIPMANTYPEBGCOLOR=${RECCOLOR[3]} ; SIPMANTYPESTR=${RECTYPESTR[3]} ;;
    (${RECNAME[4]}) SIPMANTYPEBGCOLOR=${RECCOLOR[4]} ; SIPMANTYPESTR=${RECTYPESTR[4]} ;;
    (${RECNAME[5]}) SIPMANTYPEBGCOLOR=${RECCOLOR[5]} ; SIPMANTYPESTR=${RECTYPESTR[5]} ;;
    (${RECNAME[6]}) SIPMANTYPEBGCOLOR=${RECCOLOR[6]} ; SIPMANTYPESTR=${RECTYPESTR[6]} ;;
    (${RECNAME[7]}) SIPMANTYPEBGCOLOR=${RECCOLOR[7]} ; SIPMANTYPESTR=${RECTYPESTR[7]} ;;
    (*)             SIPMANTYPEBGCOLOR=$TYPECOLOR ; SIPMANTYPESTR="-" ;;
  esac
  MACADDR=$TMAC
  #MACCOLOR=`testMacColor $MACTYPE`

  NSTATUS=`readFile $NETDIR/pollstatus.txt`
  case ${NSTATUS:20} in
    (${NMAPNAME[1]})  STATUSBGCOLOR=${NMAPCOLOR[1]} ;;
    (${NMAPNAME[2]})  STATUSBGCOLOR=${NMAPCOLOR[2]} ;;
    (${NMAPNAME[3]})  STATUSBGCOLOR=${NMAPCOLOR[3]} ;;
    (${NMAPNAME[4]})  STATUSBGCOLOR=${NMAPCOLOR[4]} ;;
    (${NMAPNAME[5]})  STATUSBGCOLOR=${NMAPCOLOR[5]} ;;
    (${NMAPNAME[6]})  STATUSBGCOLOR=${NMAPCOLOR[6]} ;;
    (${NMAPNAME[7]})  STATUSBGCOLOR=${NMAPCOLOR[7]} ;;
    (*)               STATUSBGCOLOR=${NMAPCOLOR[0]} ;;
  esac
  if [ "${NSTATUS:0:10}" = "`date +%Y-%m-%d`" ]; then
    LINESTATUS=${NSTATUS:11:8}
  else
    LINESTATUS=${NSTATUS:0:10}
  fi
  if [ -f "$NETDIR/dnsname.txt" ]; then
    DNSREC=( `readFile $NETDIR/dnsname.txt` )
    DNSNAME="${DNSREC[1]}.${DNSREC[2]}"
    case ${DNSREC[0]} in
      (${DNSTYPE[1]})     DNSBGCOLOR=${DNSCOLOR[1]} ;;
      (${DNSTYPE[2]})     DNSBGCOLOR=${DNSCOLOR[2]} ;;
      (${DNSTYPE[3]})     DNSBGCOLOR=${DNSCOLOR[3]} ;;
      (${DNSTYPE[4]})     DNSBGCOLOR=${DNSCOLOR[4]} ;;
      (${DNSTYPE[5]})     DNSBGCOLOR=${DNSCOLOR[5]} ;;
      (${DNSTYPE[6]})     DNSBGCOLOR=${DNSCOLOR[6]} ;;
      (*)                 DNSBGCOLOR=${DNSCOLOR[0]} ;;
    esac
  else
    DNSNAME=noname.nodomain
    DNSBGCOLOR=white
  fi
  if [ "${#DNSNAME}" -gt "40" ]; then
    DNSNAME="${DNSNAME:0:38}..."
  fi
  HOSTNAME=`readFile $NETDIR/hostname.txt`
  if [ "$HOSTNAME" = "-" ]; then
    HOSTNAME=`readFile $NETDIR/dhcpname.txt`
  else
    if [ "${#HOSTNAME}" -gt "30" ]; then
      HOSTNAME="${HOSTNAME:0:28}..."
    fi
  fi
  SNMPOID=( `readFile $NETDIR/snmpoid.txt` )
  FIRSTMAC=`readFile $NETDIR/firstmac.txt`
  FMACDIR="$SIM_MACDATAB/$FIRSTMAC"
  IPNETNO=`readFile $FMACDIR/ipnetnum.txt`
  INTNUM=`readFile $FMACDIR/intnum.txt`
  DMACDIR="$SIM_MACDATAB/$MACADDR"
  if [ -f $DMACDIR/switchport ]; then
    CURRSWPORT=`cat $DMACDIR/switchport | awk '{ print $1 " " $2 }'`
  else
    CURRSWPORT="unknown"
  fi
  if [ "$CURRSWPORT" != "$SWITCHNAME $SWITCHPORT" ]; then
    MACCOLOR=yellow
  else
    MACCOLOR=$TYPECOLOR
  fi
  echo "<TR BGCOLOR=$TYPECOLOR>
        <TD BGCOLOR=FFFFFF></TD>
        <TD>$VLANNO</TD>
        <TD BGCOLOR=$MANBGCOLOR ALIGN=center WIDTH=10>$DMANSTR</TD>
        <TD>$IPADDR</TD>
        <TD BGCOLOR=$MACCOLOR>$MACADDR</TD>
        <TD BGCOLOR=$SIPMANTYPEBGCOLOR>$SIPMANTYPESTR</TD>
        <TD>$NODETYPE</TD>
        <TD ALIGN=center BGCOLOR=$STATUSBGCOLOR><A HREF=$SCRIPT_NAME?mon+nodestatus+$IPADDR+$NETWORK>$LINESTATUS</A></TD>
        <TD ALIGN=right BGCOLOR=$DNSBGCOLOR>$DNSNAME</TD>
        <TD>$HOSTNAME</TD>
        <TD>${SNMPOID[0]}</TD>
        <TD>$IPNETNO/$INTNUM</TD>
        </TR>"

}

displayMacInfo ()
{
  local SWITCHNAME=$1
  echo "<TABLE>
        <TR><TD COLSPAN=12 BGCOLOR=BBBBBB ALIGN=CENTER>Switch portinfo for $SWITCHNAME, `getSnmpLoc $SWITCHNAME`.</TD></TR>
       "
  if [ "$SWITCH" != "none" ]; then 
    cat $SWITCHDEV/$SWITCH/mac-* | sort -nk 2 | while read TSWNAME TSWPORT TMAC TDATE TVLANNO TVLANNAME TPORT TDESCR
    do
      if [ "$TPORT" != "$OLDPORT" ]; then
        #echo "<TR BGCOLOR=EEEEEE ><TD>$TPORT</TD><TD COLSPAN=11>$TDESCR (vlan $TVLANNO, $TVLANNAME)</TD></TR>"
        OLDPORT=$TPORT
      fi
      IPADDR="`testIpAddr $TMAC`"
      if [ "$IPADDR" != "" ]; then
        #echo "<TR BGCOLOR=EEEEEE ><TD COLSPAN=11> dispNodeInfo IPADDR=$IPADDR, TMAC=$TMAC, TSWNAME=$TSWNAME, TPORT=$TPORT, TVLANNO=$TVLANNO.</TD></TR>"
        dispNodeInfo $IPADDR $TMAC $TSWNAME $TPORT $TVLANNO 
      else
        echo "<TR><TD></TD><TD>$TVLANNO</TD><TD></TD><TD>No ip info.</TD><TD>$TMAC</TD></TR>"
      fi
    done
  fi
  echo "</TABLE>"
}

displaySwitchInfo ()
{
  local SWITCHNAME=$1
  local SWITCHIP=none
  local SWITCHCOMM=none
  local TEST=`testSwitchName $SWITCHNAME`
  #echo "TEST $TEST.<BR>"
  if [ "$TEST" = "$SWITCHNAME" ]; then
    SWITCHIP=`cat $SWITCHDEV/$SWITCHNAME/switchip | awk '{ print $1 }'`
    SWITCHCOMM=`cat $SWITCHDEV/$SWITCHNAME/switchip | awk '{ print $2 }'`
  fi
  #echo "$SWITCHIP, $SWITCHCOMM<BR>"
  echo "<TABLE>
        <TR><TD COLSPAN=12 BGCOLOR=BBBBBB ALIGN=CENTER>Switch portinfo for 
               <A HREF=$SCRIPT_NAME?mon+nodestatus+$SWITCHIP>$SWITCHNAME</A>, `getSnmpLoc $SWITCHNAME`.
             </TD></TR>
       "

  local TMPFILE=$SIM_TMPDIR/$SWITCH.$$
  ls $SWITCHDEV/$SWITCH | while read FILE
  do
    if [ ${FILE:0:4} = "mac-" ]; then
      cat $SWITCHDEV/$SWITCH/$FILE >> $TMPFILE
    fi
  done
  if [ ! -f $TMPFILE ]; then
    echo "Removing switchinfo, no mac info found."
    rm -fR $SWITCHDEV/$SWITCH
    exit
  fi
  cat $SWITCHDEV/$SWITCH/interface-info | while read INTNO INTNAME DESCR
  do
    if [ "$SWITCHIP" != "" ]; then
      INTSTATCOLOR=`getIntStatus $SWITCHIP $SWITCHCOMM $INTNO`
    fi
    if [ -f $SWITCHDEV/$SWITCH/trunk-$INTNO ]; then
      PORTTYPE=TRUNK
      PORTCOLOR=#CCCCCC
      if [ "$DESCR" = "" ]; then
        DESCR="Trunk"
      fi
    else
      PORTTYPE=LINK
      PORTCOLOR=#EEEEEE
    fi

    echo "<TR BGCOLOR=#EEEEEE ><TD BGCOLOR=$INTSTATCOLOR>$INTNAME</TD><TD COLSPAN=11 BGCOLOR=$PORTCOLOR>$DESCR</TD></TR>"
    #echo "<TR><TD COLSPAN=2><PRE>"
    #grep -h "$SWITCHNAME " $SWITCHDEV/$SWITCH/mac-*
    #echo "</PRE></TD></TR>"
    grep -h "$SWITCHNAME $INTNO " $TMPFILE  | while read TSWNAME TSWPORT TMAC TDATE TVLANNO TVLANNAME TPORT TDESCR
    do
      IPADDR="`testIpAddr $TMAC`"
      if [ "$PORTTYPE" = "TRUNK" ]; then
        rm -f $SWITCHDEV/$SWITCH/mac-$TMAC
#	echo "<TR><TD></TD><TD>Rem</TD><TD COLSPAN=11>$TMAC</TD></TR>"
      elif [ "$IPADDR" != "" ]; then
        #echo "<TR><TD COLSPAN=11>TMAC=$TMAC TSWNAME=$TSWNAME, TPORT=$TPORT, TVLANNO=$TVLANNO.</TD></TR>"
        #dispNodeInfo $IPADDR $TMAC $TSWNAME $TSWPORT $TVLANNO 
        dispNodeInfo $IPADDR $TMAC $TSWNAME "$TPORT" $TVLANNO 
      else
        DMACDIR="$SIM_MACDATAB/$MACADDR"
        local MACCOLOR=""
        if [ -f $DMACDIR/switchport ]; then
          CURRSWPORT=`cat $DMACDIR/switchport | awk '{ print $1 " " $2 }'`
          if [ "$CURRSWPORT" != "$TSWNAME $TPORT" ]; then
            local MACCOLOR=yellow
          fi
        fi
        echo "<TR><TD></TD><TD>$TVLANNO</TD><TD></TD><TD>No ip info.</TD><TD BGCOLOR=$MACCOLOR>$TMAC</TD></TR>"
      fi
    done
    
  done
  echo "</TABLE>"
  rm $TMPFILE
}
# ------------------------- Main -----------------------------
ACTION=$1
if [ "$ACTION" = "none" ]; then
  ACTION=display
fi
SWITCH=$2
if [ "$SWITCH" = "form" ]; then
  SWITCH=$FORM_switch
fi
OPTION=$3
printDebug 1 "ACTION=$ACTION SWITCH=$SWITCH OPTION=$OPTION"
echo "
  <A HREF=$SCRIPT_NAME?switch+display+$SWITCH+$OPTION>Display</A> -
  <A HREF=$SCRIPT_NAME?switch+print+$SWITCH+$OPTION>Print</A> -
  <A HREF=$SCRIPT_NAME?switch+raw+$SWITCH+$OPTION>Raw</A> 
     </TD></TR></TABLE><CENTER>"

case $ACTION in
(display) displaySwitches $SWITCH
          if [ -f $SWITCHDEV/$SWITCH/interface-info ]; then
            #echo "<BR>displaySwitchInfo $SWITCH<BR>"
            displaySwitchInfo $SWITCH
          else
            displayMacInfo $SWITCH
          fi;;
(print)   if [ -f $SWITCHDEV/$SWITCH/interface-info ]; then
            displaySwitchInfo $SWITCH
          else
	    displayMacInfo $SWITCH
	  fi
  ;;
(info)   displayMacInfo $SWITCH ;;
(raw)     displaySwitches $SWITCH; displayRaw $SWITCH ;;
(*) echo "Oops, action $ACTION not configured." ;;
esac
